#include "../inc/hart_slave_req.h"
#include "../inc/hart_slave_frame.h"
#ifndef STM32
static uint32_t sys_millis(void)
{
    return 0;
}
#else
#include "delay.h"
#endif

static BOOL falsh_write(void)
{
    return hart_storage_write(HART_EEPROM_ADDRESS_USER, (uint8_t *)&hart_device_attribute.flash_variable_user, sizeof(hart_storage_variable_user_t));
}

static BOOL falsh_read(void)
{
    return hart_storage_read(HART_EEPROM_ADDRESS_USER, (uint8_t *)&hart_device_attribute.flash_variable_user, sizeof(hart_storage_variable_user_t));
}

/**
 * @brief 自定义参数读取
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 * @note
 */
static BOOL hart_slave_command_130_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    if (req->data_length < sizeof(hart_command_130_t))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_5;
        return TRUE;
    }
    if (req->data.command_130.code >= COMMAND_CUSTOM_CODE_MAX)
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_19;
        return TRUE;
    }

    hart_custom_variable_t rsp;
    osel_memset((uint8_t *)&rsp, 0, sizeof(hart_custom_variable_t));
    rsp.code = req->data.command_130.code;
    if (FALSE == user_common_event(HART_COMMAND_130_EVENT, (void *)&rsp))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_32;
        return TRUE;
    }

    // 回复数据
    *resp->data_p++ = rsp.code;
    *resp->data_p++ = rsp.data_length;
    osel_memcpy(resp->data_p, (uint8_t *)&rsp.data.buf, rsp.data_length);
    resp->data_p += rsp.data_length;
    return TRUE;
}

/**
 * @brief 自定义参数设置
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 * @note
 */
static BOOL hart_slave_command_131_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    if (req->data_length < 2)
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_5;
        return TRUE;
    }
    if (req->data.command_131.code >= COMMAND_CUSTOM_CODE_MAX)
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_19;
        return TRUE;
    }

    hart_custom_variable_t rsp;
    osel_memset((uint8_t *)&rsp, 0, sizeof(hart_custom_variable_t));
    rsp.code = req->data.command_131.code;
    rsp.data_length = req->data.command_131.data_length;
    osel_memcpy((uint8_t *)rsp.data.buf, req->data.command_131.data, rsp.data_length);

    if (FALSE == user_common_event(HART_COMMAND_131_EVENT, (void *)&rsp))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_32;
        return TRUE;
    }
    configuration_count_update();

    // 回复数据
    *resp->data_p++ = rsp.code;
    *resp->data_p++ = rsp.data_length;
    osel_memcpy(resp->data_p, (uint8_t *)&rsp.data.buf, rsp.data_length);
    resp->data_p += rsp.data_length;
    return TRUE;
}

/**
 * @brief 自定义参数读取(拓展)
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 * @note
 */
static BOOL hart_slave_command_132_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    if (req->data_length < sizeof(hart_command_132_t))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_5;
        return TRUE;
    }
    if (req->data.command_132.code >= COMMAND_CUSTOM_CODE_MAX)
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_19;
        return TRUE;
    }

    hart_custom_variable_t rsp;
    osel_memset((uint8_t *)&rsp, 0, sizeof(hart_custom_variable_t));
    rsp.code = req->data.command_132.code;
    if (FALSE == user_common_event(HART_COMMAND_132_EVENT, (void *)&rsp))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_32;
        return TRUE;
    }

    // 回复数据
    *resp->data_p++ = rsp.code;
    *resp->data_p++ = rsp.data_length;
    osel_memcpy(resp->data_p, (uint8_t *)&rsp.data.buf, rsp.data_length);
    resp->data_p += rsp.data_length;
    return TRUE;
}

/**
 * @brief 自定义参数设置(拓展)
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 * @note
 */
static BOOL hart_slave_command_133_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    if (req->data_length < 2)
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_5;
        return TRUE;
    }
    if (req->data.command_133.code >= COMMAND_CUSTOM_CODE_MAX)
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_19;
        return TRUE;
    }

    hart_custom_variable_t rsp;
    osel_memset((uint8_t *)&rsp, 0, sizeof(hart_custom_variable_t));
    rsp.code = req->data.command_133.code;
    rsp.data_length = req->data.command_133.data_length;
    osel_memcpy((uint8_t *)rsp.data.buf, req->data.command_133.data, rsp.data_length);

    if (FALSE == user_common_event(HART_COMMAND_133_EVENT, (void *)&rsp))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_32;
        return TRUE;
    }
    configuration_count_update();

    // 回复数据
    *resp->data_p++ = rsp.code;
    *resp->data_p++ = rsp.data_length;
    osel_memcpy(resp->data_p, (uint8_t *)&rsp.data.buf, rsp.data_length);
    resp->data_p += rsp.data_length;
    return TRUE;
}

/**
 * @brief 重置参数
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 * @note
 */
static BOOL hart_slave_command_140_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    return TRUE;
}

/**
 * @brief 设置写入保护
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 * @note
 */
static BOOL hart_slave_command_141_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    uint8_t write_protect_code = req->data.command_141.write_protect_code;
    if (write_protect_code == 0x00)
    {
        hart_device_attribute.flash_variable.write_protect_code = WRITE_PROTECT_CODE_NOT_WRITE_PROTECT;
    }
    else if (write_protect_code == 0x01)
    {
        hart_device_attribute.flash_variable.write_protect_code = WRITE_PROTECT_CODE_WRITE_PROTECT;
    }
    else
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_65;
        return TRUE;
    }

    // 保存到flash
    if (FALSE == falsh_write())
    {
        falsh_read();
        resp->code = RESPONSE_COMMUNICATION_CODE_8;
        return TRUE;
    }

    // 回复数据
    *resp->data_p++ = hart_device_attribute.flash_variable.write_protect_code;
    return TRUE;
}
/**
 * @brief数据样品型号(气开\气关)
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 */
static BOOL hart_slave_command_254_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    if (req->data_length < sizeof(hart_command_254_t))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_5;
        return TRUE;
    }
    hart_device_attribute.flash_variable_user.gas_opening_closing = req->data.command_254.model;
    // 保存到flash
    if (FALSE == falsh_write())
    {
        falsh_read();
        resp->code = RESPONSE_COMMUNICATION_CODE_8;
        return TRUE;
    }

    // 回复数据
    *resp->data_p++ = hart_device_attribute.flash_variable_user.gas_opening_closing;
    LOG_PRINT("flash_variable:%d", sizeof(hart_device_attribute.flash_variable));
    LOG_PRINT("flash_variable_user:%d", sizeof(hart_device_attribute.flash_variable_user));
    return TRUE;
}

/**
 * @brief读取模拟量数值
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 */
static BOOL hart_slave_command_255_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    float32_u f;
    uint16_t d16;
    if (FALSE == user_common_event(HART_COMMAND_UPDATE_EVENT, NULL))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_32;
    }
    analog_quantity_t *analog_quantity = &hart_device_attribute.dynamics.dynamics_user.analog_quantity;
    // 输入电流
    f.f = *analog_quantity->input_current;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 阀位反馈
    d16 = *analog_quantity->valve_feedback;
    d16 = S2B_UINT16(d16);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // 气压源压力
    d16 = *analog_quantity->atmospheric_pressure_source;
    d16 = S2B_UINT16(d16);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // A口压力
    d16 = *analog_quantity->pressure_at_port_a;
    d16 = S2B_UINT16(d16);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // B口压力
    d16 = *analog_quantity->pressure_at_port_b;
    d16 = S2B_UINT16(d16);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // 放大器回路
    d16 = *analog_quantity->amplifier_circuit;
    d16 = S2B_UINT16(d16);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // 内置温度
    f.f = *analog_quantity->built_in_temperature;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // IP输出检测
    d16 = *analog_quantity->ip_output_detection;
    d16 = S2B_UINT16(d16);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // 阀位百分比
    f.f = *analog_quantity->valve_percentage;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);

    return TRUE;
}

/**
 * @brief读取转化的数值
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 */
static BOOL hart_slave_command_256_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    float32_u f;
    uint16_t d16;
    if (FALSE == user_common_event(HART_COMMAND_UPDATE_EVENT, NULL))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_32;
    }
    digital_quantity_t *digital_quantity = &hart_device_attribute.dynamics.dynamics_user.digital_quantity;
    // 输入电流
    f.f = *digital_quantity->input_current;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 阀位反馈
    d16 = *digital_quantity->valve_feedback;
    d16 = S2B_UINT16(d16);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // 气压源压力
    d16 = *digital_quantity->atmospheric_pressure_source;
    d16 = S2B_UINT16(d16);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // A口压力
    d16 = *digital_quantity->pressure_at_port_a;
    d16 = S2B_UINT16(d16);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // B口压力
    d16 = *digital_quantity->pressure_at_port_b;
    d16 = S2B_UINT16(d16);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // 放大器回路
    d16 = *digital_quantity->amplifier_circuit;
    d16 = S2B_UINT16(d16);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // 内置温度
    f.f = *digital_quantity->built_in_temperature;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // IP输出检测
    d16 = *digital_quantity->ip_output_detection;
    d16 = S2B_UINT16(d16);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);

    // 目标行程
    f.f = *digital_quantity->target_row;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 当前行程
    f.f = *digital_quantity->current_row;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 摩擦力
    f.f = *digital_quantity->friction;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 弹簧力
    f.f = *digital_quantity->spring_force;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);

    return TRUE;
}

/**
 * @brief寻找、计算P / I / D
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 */
static BOOL hart_slave_command_257_req(const hart_command_req_t *const req, hart_response_t *resp)
{

    if (FALSE == user_common_event(HART_COMMAND_257_EVENT, NULL))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_32;
    }
    return TRUE;
}

/**
 * @brief计算 摩擦力
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 */
static BOOL hart_slave_command_258_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    if (FALSE == user_common_event(HART_COMMAND_258_EVENT, NULL))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_32;
    }
    return TRUE;
}

/**
 * @brief计算 弹簧力
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 */
static BOOL hart_slave_command_259_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    if (FALSE == user_common_event(HART_COMMAND_259_EVENT, NULL))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_32;
    }
    return TRUE;
}

/**
 * @brief编写参数配置
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 */
static BOOL hart_slave_command_260_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    if (req->data_length < sizeof(hart_command_260_t))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_5;
        return TRUE;
    }
    float32_u f;
    hart_storage_variable_user_t *flash_variable_user = &hart_device_attribute.flash_variable_user;
    hart_command_260_t *command_260 = (hart_command_260_t *)&req->data.command_260;
    hart_command_260_t command_260_tmp;
    osel_memcpy((uint8_t *)&command_260_tmp, (uint8_t *)command_260, sizeof(hart_command_260_t));
    /* 处理大小端 */

    // 控制模式
    // 阀位控制
    f.f = command_260->threshold_control;
    f.c = B2S_UINT32(f.c);
    command_260->threshold_control = f.f;
    // 放大器回路控制
    f.f = command_260->amplifier_circuit_control;
    f.c = B2S_UINT32(f.c);
    command_260->amplifier_circuit_control = f.f;
    // A口压力
    command_260->pressure_A = B2S_UINT16(command_260->pressure_A);
    // B口压力
    command_260->pressure_B = B2S_UINT16(command_260->pressure_B);
    // 位置作用选择
    // 位置反馈--P
    f.f = command_260->position_feedback_P;
    f.c = B2S_UINT32(f.c);
    command_260->position_feedback_P = f.f;
    // 位置反馈--I
    f.f = command_260->position_feedback_I;
    f.c = B2S_UINT32(f.c);
    command_260->position_feedback_I = f.f;
    // 位置反馈--D
    f.f = command_260->position_feedback_D;
    f.c = B2S_UINT32(f.c);
    command_260->position_feedback_D = f.f;
    // 位置反馈-放大器回路控
    command_260->position_feedback_amplifier_circuit_control_gain = B2S_UINT16(command_260->position_feedback_amplifier_circuit_control_gain);
    // 压力作用选择
    // 压力反馈--P
    f.f = command_260->pressure_feedback_P;
    f.c = B2S_UINT32(f.c);
    command_260->pressure_feedback_P = f.f;
    // 压力反馈--I
    f.f = command_260->pressure_feedback_I;
    f.c = B2S_UINT32(f.c);
    command_260->pressure_feedback_I = f.f;
    // 压力反馈--D
    f.f = command_260->pressure_feedback_D;
    f.c = B2S_UINT32(f.c);
    command_260->pressure_feedback_D = f.f;
    // 压力反馈-放大器回路控
    command_260->pressure_feedback_amplifier_circuit_control_gain = B2S_UINT16(command_260->pressure_feedback_amplifier_circuit_control_gain);
    // 阀门型号
    // 弹簧系数
    f.f = command_260->spring_coefficient;
    f.c = B2S_UINT32(f.c);
    command_260->spring_coefficient = f.f;
    // 气室活塞面积
    f.f = command_260->gas_inlet_cross_sectional_area;
    f.c = B2S_UINT32(f.c);
    command_260->gas_inlet_cross_sectional_area = f.f;
    // 气开/气关

    osel_memcpy((uint8_t *)flash_variable_user, (uint8_t *)&req->data.command_260, sizeof(hart_command_260_t));
    // 保存到flash
    if (FALSE == falsh_write())
    {
        falsh_read();
        resp->code = RESPONSE_COMMUNICATION_CODE_8;
        return TRUE;
    }
    // 回复数据
    osel_memcpy(resp->data_p, (uint8_t *)&command_260_tmp, sizeof(hart_command_260_t));
    resp->data_p += sizeof(hart_command_260_t);

    return TRUE;
}

/**
 * @brief读取参数配置
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 */
static BOOL hart_slave_command_261_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    float32_u f;
    uint16_t d16;
    hart_storage_variable_user_t *flash_variable_user = &hart_device_attribute.flash_variable_user;

    /* 处理大小端 */

    // 控制模式
    *resp->data_p++ = flash_variable_user->control_mode;
    // 阀位控制
    f.f = flash_variable_user->threshold_control;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 放大器回路控制
    f.f = flash_variable_user->amplifier_circuit_control;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // A口压力
    d16 = S2B_UINT16(flash_variable_user->pressure_A);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // B口压力
    d16 = S2B_UINT16(flash_variable_user->pressure_B);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // 位置作用选择
    *resp->data_p++ = flash_variable_user->position_selector;
    // 位置反馈--P
    f.f = flash_variable_user->position_feedback_P;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 位置反馈--I
    f.f = flash_variable_user->position_feedback_I;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 位置反馈--D
    f.f = flash_variable_user->position_feedback_D;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 位置反馈-放大器回路控制增益
    d16 = S2B_UINT16(flash_variable_user->position_feedback_amplifier_circuit_control_gain);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // 压力作用选择
    *resp->data_p++ = flash_variable_user->pressure_selector;
    // 压力反馈--P
    f.f = flash_variable_user->pressure_feedback_P;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 压力反馈--I
    f.f = flash_variable_user->pressure_feedback_I;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 压力反馈--D
    f.f = flash_variable_user->pressure_feedback_D;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 压力反馈-放大器回路控制增益
    d16 = S2B_UINT16(flash_variable_user->pressure_feedback_amplifier_circuit_control_gain);
    osel_memcpy(resp->data_p, (uint8_t *)&d16, sizeof(uint16_t));
    resp->data_p += sizeof(uint16_t);
    // 阀门型号
    osel_memcpy(resp->data_p, (uint8_t *)flash_variable_user->valve_model, ARRAY_LEN(flash_variable_user->valve_model));
    resp->data_p += ARRAY_LEN(flash_variable_user->valve_model);
    // 弹簧系数
    f.f = flash_variable_user->spring_coefficient;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 气室活塞面积
    f.f = flash_variable_user->gas_inlet_cross_sectional_area;
    f.c = S2B_UINT32(f.c);
    osel_memcpy(resp->data_p, (uint8_t *)&f.c, sizeof(int32_t));
    resp->data_p += sizeof(int32_t);
    // 气开/气关
    *resp->data_p++ = flash_variable_user->gas_opening_closing;
    return TRUE;
}

/**
 * @brief 测试指令
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 */
static BOOL hart_slave_command_500_req(const hart_command_req_t *const req, hart_response_t *resp)
{
    if (req->data_length < 2)
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_5;
        return TRUE;
    }
    hart_user_req_t user_req;

    user_req.data.command_500.test_code = req->data.command_500.test_code;
    user_req.data.command_500.data_length = req->data.command_500.data_length;

    *resp->data_p++ = req->data.command_500.test_code;

    if (req->data.command_500.data_length > 0)
    {
        osel_memcpy(user_req.data.command_500.data, req->data.command_500.data, user_req.data.command_500.data_length);
    }

    user_req.code = RESPONSE_COMMUNICATION_CODE_0;

    if (FALSE == user_common_event(HART_COMMAND_500_EVENT, (void *)&user_req))
    {
        resp->code = RESPONSE_COMMUNICATION_CODE_32;
    }

    *resp->data_p++ = user_req.data.command_500.data_length;
    if (user_req.data.command_500.data_length > 0)
    {
        osel_memcpy(resp->data_p, user_req.data.command_500.data, user_req.data.command_500.data_length);
        resp->data_p += user_req.data.command_500.data_length;
    }

    if (user_req.code == RESPONSE_COMMUNICATION_CODE_127)
    {
        resp->cache_data.uuid = user_req.uuid;
        resp->cache_data.hart_cache_time = sys_millis() + KEY_CACHE_TIME_MS;
        resp->code = RESPONSE_COMMUNICATION_CODE_127;
        return FALSE;
    }
    return TRUE;
}

/**
 * @brief 突发测试数据
 * @param {hart_command_req_t} *req
 * @param {hart_response_t} *resp
 * @return {*}
 */
void hart_slave_command_501_req(burst_test_data_e code, uint8_t data_length, uint8_t *data, hart_cache_t *cache_data)
{
    hart_command_501_t data_tmp;
    uint8_t length = 1 + 1 + data_length; // 1字节是子命令，1字节是数据长度
    if (data_length > ARRAY_LEN(data_tmp.data))
    {
        DBG_ASSERT(FALSE __DBG_LINE);
    }

    hart_delimiter_u *delimiter = &cache_data->delimiter;
    osel_memset((uint8_t *)&data_tmp, 0, sizeof(hart_command_501_t));

    delimiter->bits.frame_type = HART_BURST_FRAME;
    delimiter->bits.addr_type = HART_DELIMITER_TYPE_LONG;
    delimiter->bits.ext_bytes = 1;
    get_address(HART_DELIMITER_TYPE_LONG, cache_data->address, &cache_data->address_length); // 获取本机的地址
    cache_data->command = HART_COMMAND_501;
    cache_data->command_length = 2;
    cache_data->code = RESPONSE_COMMUNICATION_CODE_0;

    data_tmp.test_code = code;
    data_tmp.data_length = data_length;
    osel_memcpy(data_tmp.data, data, data_length);

    cache_data->payload = osel_mem_alloc(length);
    cache_data->payload_length = length;
    osel_memcpy(cache_data->payload, (uint8_t *)&data_tmp, length);
    return;
}

/**
 * @brief 用户自定义指令初始化
 * @return {*}
 */
void hart_slave_req_init_user(void)
{
    // 128-253 设备特定
    hart_command_ptr_arr[HART_COMMAND_130] = hart_slave_command_130_req; // 自定义参数读取
    hart_command_ptr_arr[HART_COMMAND_131] = hart_slave_command_131_req; // 自定义参数设置
    hart_command_ptr_arr[HART_COMMAND_132] = hart_slave_command_132_req; // 自定义参数读取(拓展)
    hart_command_ptr_arr[HART_COMMAND_133] = hart_slave_command_133_req; // 自定义参数设置(拓展)
    hart_command_ptr_arr[HART_COMMAND_140] = hart_slave_command_140_req; // 重置参数
    hart_command_ptr_arr[HART_COMMAND_141] = hart_slave_command_141_req; // 设置写入保护
    // 254-511 自定义指令
    hart_command_ptr_arr[HART_COMMAND_254] = hart_slave_command_254_req; // 数据样品型号(气开\气关)
    hart_command_ptr_arr[HART_COMMAND_255] = hart_slave_command_255_req; // 读取模拟量数值
    hart_command_ptr_arr[HART_COMMAND_256] = hart_slave_command_256_req; // 读取转化的数值
    hart_command_ptr_arr[HART_COMMAND_257] = hart_slave_command_257_req; // 寻找、计算P / I / D
    hart_command_ptr_arr[HART_COMMAND_258] = hart_slave_command_258_req; // 计算 摩擦力
    hart_command_ptr_arr[HART_COMMAND_259] = hart_slave_command_259_req; // 计算 弹簧力
    hart_command_ptr_arr[HART_COMMAND_260] = hart_slave_command_260_req; // 编写参数配置
    hart_command_ptr_arr[HART_COMMAND_261] = hart_slave_command_261_req; // 读取参数配置
    hart_command_ptr_arr[HART_COMMAND_500] = hart_slave_command_500_req; // 测试指令
}
